package com.uniview.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.TimeToLive;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.util.List;
import java.util.Map;

/**
 * @Author lux
 * @date: 2019/12/12
 * @desc: 事件控制器
 */
@RestController
@RequestMapping("/event")
public class EventController {
    @Autowired
    ReactiveRedisTemplate reactiveRedisTemplate;
    /**
     * @desc 保存事件
     * @param events
     */
    @RequestMapping(value = "/save", consumes = MediaType.APPLICATION_STREAM_JSON_VALUE)
    public Flux<Void> saveEvent(@RequestBody Flux<Map<String,Object>> events){
        return events.flatMap(city-> reactiveRedisTemplate.opsForSet().add("event",city));

    }
    @RequestMapping(value = "/get", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
    public Flux<Map<String,Object>> getEvent(@RequestParam("key") String key){
        return reactiveRedisTemplate.opsForSet().members(key).delayElements(Duration.ofSeconds(1));
    }

}
